package com.whacos.modules.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whacos.common.controller.BaseController;
import com.whacos.common.utils.BeanUtils;
import com.whacos.common.utils.ObjectUtils;
import com.whacos.common.utils.StringUtils;
import com.whacos.common.vo.ResponseVO;
import com.whacos.modules.system.entity.Log;
import com.whacos.modules.system.service.LogService;
import com.whacos.modules.system.vo.LogVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xiongdun
 * @description SysLogController
 * @since 2018/11/17 17:11
 */
@Controller
@RequestMapping("/sys/log")
@Slf4j
public class LogController extends BaseController {

    private static final String PREFIX = "sys/log";

    @Autowired
    private LogService logService;

    // @Log("跳转日志信息列表")
    @GetMapping()
    @RequiresPermissions("sys:log:log")
    public String log() {
        return PREFIX + "/log";
    }

    // @Log("查询日志信息列表")
    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("sys:log:log")
    public IPage<LogVO> list(LogVO logVO) {
        IPage<LogVO> sysLogVOPage = new Page<>();
        try {
            // 请求参数
            // 查询数据
            IPage<Log> sysLogPage = logService.page(new Page<>(logVO.getPageNum(), logVO.getPageSize()),
                    Wrappers.lambdaQuery(new Log())
                    .eq(StringUtils.isNotEmpty(logVO.getUsername()), Log::getCreatedUsername, logVO.getUsername())
                    .eq(ObjectUtils.isNotEmpty(logVO.getCreatedDate()), Log::getCreatedDate, logVO.getCreatedDate())
                    .orderByAsc(Log::getCreatedDate));

            // 返回结果
            BeanUtils.copyPage(sysLogPage, sysLogVOPage);
            List<LogVO> logVOList = new ArrayList<>();
            sysLogPage.getRecords().forEach(source -> {
                LogVO target = new LogVO();
                BeanUtils.copyProperties(source, target);
                logVOList.add(target);
            });
            sysLogVOPage.setRecords(logVOList);
        } catch (Exception e) {
            log.error("查询日志列表异常，异常原因：" + e.getMessage(), e);
        }
        return sysLogVOPage;
    }

    @com.whacos.common.annotations.Log("删除日志信息")
    @ResponseBody
    @PostMapping("/remove")
    @RequiresPermissions("sys:log:remove")
    public ResponseVO remove(@RequestParam(value = "id") Long id) {
        try {
            if (logService.removeById(id)) {
                return ResponseVO.success();
            }
        } catch (Exception e) {
            log.error("删除日志异常，异常原因：" + e.getMessage(), e);
            return ResponseVO.error(e.getMessage());
        }
        return ResponseVO.success();
    }

    @com.whacos.common.annotations.Log("批量删除日志信息")
    @ResponseBody
    @PostMapping("/batchRemove")
    @RequiresPermissions("sys:log:batchRemove")
    public ResponseVO batchRemove(@RequestParam(value = "ids[]") List<Long> ids) {
        try {
            if (logService.removeByIds(ids)) {
                return ResponseVO.success();
            }
        } catch (Exception e) {
            log.error("批量删除日志异常，异常原因：" + e.getMessage(), e);
            return ResponseVO.error(e.getMessage());
        }
        return ResponseVO.success();
    }

    @com.whacos.common.annotations.Log("清空日志信息")
    @ResponseBody
    @PostMapping("/clear")
    @RequiresPermissions("sys:log:clear")
    public ResponseVO clear() {
        try {
            this.logService.clear();
        } catch (Exception e) {
            log.error("清空日志信息异常，异常原因：" + e.getMessage(), e);
            return ResponseVO.error(e.getMessage());
        }
        return ResponseVO.success();
    }
}
